package com.toivan.mt.fragment;

import android.os.Bundle;


import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.shizhefei.fragment.LazyFragment;
import com.toivan.mt.R;
import com.toivan.mt.adapter.MtFestivalStickerAdapter;
import com.toivan.mt.model.MtDynamicSticker;
import com.toivan.mt.utils.MtCallback;
import com.toivan.mt.utils.MtJsonParser;
import com.toivan.sdk.MtSDK;

import java.util.ArrayList;
import java.util.List;

public class MtFestivalStickerFragment extends LazyFragment {
    private final List<MtDynamicSticker> list = new ArrayList<>();
    private MtFestivalStickerAdapter adapter;



    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);

        setContentView(R.layout.fragment_sticker_recyclerview);

        if (getContext() == null) return;

        list.clear();
        List<MtDynamicSticker> stickers = MtJsonParser.getInstance().getAllDynamicStickers();

        if (stickers == null || stickers.size() == 0) {
            MtJsonParser.getInstance().getDynamicStickers( new MtCallback<List<MtDynamicSticker>>() {
                @Override
                public void callback(List<MtDynamicSticker> bean) {
                    if (bean != null) {
                        addSticker(bean);
                        showSticker();
                    }
                }
            });
        } else {
            addSticker(stickers);
            showSticker();
        }
    }

    private void addSticker(List<MtDynamicSticker> stickers) {
        for (MtDynamicSticker sticker : stickers) {
            if (sticker != null) {
                if (sticker.getCategory().equals(MtDynamicSticker.CATEGORY_FESTIVAL)) {
                    list.add(sticker);
                }
            }
        }
    }

    private void showSticker() {
        RecyclerView stickerRV = (RecyclerView) findViewById(R.id.recyclerView);
        adapter = new MtFestivalStickerAdapter(list);
        stickerRV.setLayoutManager(new GridLayoutManager(getContext(), 5));
        stickerRV.setAdapter(adapter);
    }

    public void cancelSticker() {
        if (adapter != null) {
            adapter.cancelSelected();
        }
    }



}


